<?php
namespace app\teacher\controller;

use app\teacher\controller\PublicController;
use app\common\model\TemplateModel;
use app\common\model\TemplateAttrModel;

//模板
class CountController extends PublicController
{

    protected $targets = ["_blank" => "新标签页打开", "_self" => "本窗口打开"];

	public function __construct(){
		parent::__construct();
		$this->check();
		//$this->ids = $this->install();
	}
	//数据总览
	public function index(){
		$a_year=date("Y", strtotime("-1 year"));
		$b_year=date("Y", strtotime("+1 year"));
		$month=date('m');
		if($month>8){
			$from_date=date('Y')."-9-1 00:00:00";
			$end_date=$b_year."-9-1 00:00:00";
		}else{
			$from_date=$a_year."-9-1 00:00:00";
			$end_date=date('Y')."-9-1 00:00:00";
		}
		$cm=date('Y-m-d',strtotime('-1 monday', time()));//上周一
		$dm=date('Y-m-d',(time()-((date('w',time())==0?7:date('w',time()))-1)*24*3600));//本周一
		/*学生数据*/
		$schoolID=db("app_user")->field("schoolID")->where(['id'=>$this->user_id])->find();
		$sdata=db("app_user")->field("id")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1])->select()->toArray();
		$array = [];
		foreach ($sdata as $key => $value) {
			$array[] = $value['id'];
		}
		$string = implode(',',$array);
		$student['totalcount']=db("app_user")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1])->count();
		$student['currentyearcount']=db("app_user")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1])->where("createTime>='$from_date' and createTime<'$end_date'")->count();
		$student['totaluse_num']=db("app_user_log")->where("userId in($string) and createTime>='$cm' and createTime<'$dm'")->count();
		/*教师数据*/
		$teacher['totalcount']=db("app_user")->where(['schoolID'=>$schoolID['schoolID'],'role'=>2])->count();
		$allid= $this->install();
		$teacher['currentyearcount']=db("app_user")->where(['schoolID'=>$schoolID['schoolID'],'role'=>2])->where("createTime>='$from_date' and createTime<'$end_date'")->count();
		$teacher['totaluse_num']=db("app_user_log")->where("userId in($allid) and createTime>='$cm' and createTime<'$dm'")->count();
		/*课题数据*/		
		$classdata['totalrecord']=db("record_class")->where("t_user_id in($allid) and status in(1,2)")->count();
		/*素材数据*/		
		$material['currentyearmaterial']=db("material")->where(" userId in($string) and createTime>='$from_date' and createTime<'$end_date'")->count();
		$material['activation_num']=db("anser")->where("userId in($string) and createTime>='$from_date' and createTime<'$end_date'")->sum("activation_num");
		$material['weekmaterial']=db("material")->where("userId in($string) and createTime>='$cm' and createTime<'$dm'")->count();
		
		//以下为柱状图
		$cm2=date('Y-m-d',strtotime('-2 monday', time()));//上上周一
		$dm2=date('Y-m-d',strtotime('-1 monday', time()));//上周一		
		$cm3=date('Y-m-d',strtotime('-3 monday', time()));//上上上周一
		$dm3=date('Y-m-d',strtotime('-2 monday', time()));//上上周一
		$cm4=date('Y-m-d',strtotime('-4 monday', time()));//上上上上周一
		$dm4=date('Y-m-d',strtotime('-3 monday', time()));//上上上周一
		/*学生*/
		$student['week1']=$student['totaluse_num'];//第一周
		$student['week2']=db("app_user_log")->where("userId in($string) and createTime>='$cm2' and createTime<'$dm2'")->count();//第二周
		$student['week3']=db("app_user_log")->where("userId in($string) and createTime>='$cm3' and createTime<'$dm3'")->count();//第三周
		$student['week4']=db("app_user_log")->where("userId in($string) and createTime>='$cm4' and createTime<'$dm4'")->count();//第四周
		//学生一年级第一周
		$onegrad_data=db("app_user")->field("id")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1,'grad'=>'一年级'])->select()->toArray();
		$onegrad_array = [];
		foreach ($onegrad_data as $key1 => $value1) {
			$onegrad_array[] = $value1['id'];
		}
		$onegrad_string = implode(',',$onegrad_array);
		if($onegrad_string==""){
			$student['onegrad_num1']=0;
			$student['onegrad_num2']=0;
			$student['onegrad_num3']=0;
			$student['onegrad_num4']=0;	
		}else{
			$student['onegrad_num1']=db("app_user_log")->where("userId in($onegrad_string) and createTime>='$cm' and createTime<'$dm'")->count();
			$student['onegrad_num2']=db("app_user_log")->where("userId in($onegrad_string) and createTime>='$cm2' and createTime<'$dm2'")->count();
			$student['onegrad_num3']=db("app_user_log")->where("userId in($onegrad_string) and createTime>='$cm3' and createTime<'$dm3'")->count();
			$student['onegrad_num4']=db("app_user_log")->where("userId in($onegrad_string) and createTime>='$cm4' and createTime<'$dm4'")->count();
		}
		//学生二年级第一周
		$twograd_data=db("app_user")->field("id")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1,'grad'=>'二年级'])->select()->toArray();
		$twograd_array = [];
		foreach ($twograd_data as $key2 => $value2) {
			$twograd_array[] = $value2['id'];
		}
		$twograd_string = implode(',',$twograd_array);
		if($twograd_string==""){
			$student['twograd_num1']=0;
			$student['twograd_num2']=0;
			$student['twograd_num3']=0;
			$student['twograd_num4']=0;
		}else{
			$student['twograd_num1']=db("app_user_log")->where("userId in($twograd_string) and createTime>='$cm' and createTime<'$dm'")->count();
			$student['twograd_num2']=db("app_user_log")->where("userId in($twograd_string) and createTime>='$cm2' and createTime<'$dm2'")->count();
			$student['twograd_num3']=db("app_user_log")->where("userId in($twograd_string) and createTime>='$cm3' and createTime<'$dm3'")->count();
			$student['twograd_num4']=db("app_user_log")->where("userId in($twograd_string) and createTime>='$cm4' and createTime<'$dm4'")->count();
		}
		//学生三年级第一周
		$threegrad_data=db("app_user")->field("id")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1,'grad'=>'三年级'])->select()->toArray();
		$threegrad_array = [];
		//dump($threegrad_data);
		foreach ($threegrad_data as $key3 => $value3) {
			$threegrad_array[] = $value3['id'];
		}
		$threegrad_string = implode(',',$threegrad_array);
		if($threegrad_string==""){
			$student['threegrad_num1']=0;
			$student['threegrad_num2']=0;
			$student['threegrad_num3']=0;
			$student['threegrad_num4']=0;
		}else{
			$student['threegrad_num1']=db("app_user_log")->where("userId in($threegrad_string) and createTime>='$cm' and createTime<'$dm'")->count();
			$student['threegrad_num2']=db("app_user_log")->where("userId in($threegrad_string) and createTime>='$cm2' and createTime<'$dm2'")->count();
			$student['threegrad_num3']=db("app_user_log")->where("userId in($threegrad_string) and createTime>='$cm3' and createTime<'$dm3'")->count();
			$student['threegrad_num4']=db("app_user_log")->where("userId in($threegrad_string) and createTime>='$cm4' and createTime<'$dm4'")->count();
		}
		//学生四年级第一周
		$fourgrad_data=db("app_user")->field("id")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1,'grad'=>'四年级'])->select()->toArray();
		$fourgrad_array = [];
		foreach ($fourgrad_data as $key4 => $value4) {
			$fourgrad_array[] = $value4['id'];
		}
		$fourgrad_string = implode(',',$fourgrad_array);
		if($fourgrad_string==""){
			$student['fourgrad_num1']=0;
			$student['fourgrad_num2']=0;
			$student['fourgrad_num3']=0;
			$student['fourgrad_num4']=0;
		}else{
			$student['fourgrad_num1']=db("app_user_log")->where("userId in($fourgrad_string) and createTime>='$cm' and createTime<'$dm'")->count();
			$student['fourgrad_num2']=db("app_user_log")->where("userId in($fourgrad_string) and createTime>='$cm2' and createTime<'$dm2'")->count();
			$student['fourgrad_num3']=db("app_user_log")->where("userId in($fourgrad_string) and createTime>='$cm3' and createTime<'$dm3'")->count();
			$student['fourgrad_num4']=db("app_user_log")->where("userId in($fourgrad_string) and createTime>='$cm4' and createTime<'$dm4'")->count();
		}
		//学生五年级第一周
		$fivegrad_data=db("app_user")->field("id")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1,'grad'=>'五年级'])->select()->toArray();
		$fivegrad_array = [];
		foreach ($fivegrad_data as $key5 => $value5) {
			$fivegrad_array[] = $value5['id'];
		}
		$fivegrad_string = implode(',',$fivegrad_array);
		if($fivegrad_string==""){
			$student['fivegrad_num1']=0;
			$student['fivegrad_num2']=0;
			$student['fivegrad_num3']=0;
			$student['fivegrad_num4']=0;
		}else{
			$student['fivegrad_num1']=db("app_user_log")->where("userId in($fivegrad_string) and createTime>='$cm' and createTime<'$dm'")->count();
			$student['fivegrad_num2']=db("app_user_log")->where("userId in($fivegrad_string) and createTime>='$cm2' and createTime<'$dm2'")->count();
			$student['fivegrad_num3']=db("app_user_log")->where("userId in($fivegrad_string) and createTime>='$cm3' and createTime<'$dm3'")->count();
			$student['fivegrad_num4']=db("app_user_log")->where("userId in($fivegrad_string) and createTime>='$cm4' and createTime<'$dm4'")->count();
		}
		//学生六年级第一周
		$sixgrad_data=db("app_user")->field("id")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1,'grad'=>'六年级'])->select()->toArray();
		$sixgrad_array = [];
		foreach ($sixgrad_data as $key6 => $value6) {
			$sixgrad_array[] = $value6['id'];
		}
		$sixgrad_string = implode(',',$sixgrad_array);
		if($sixgrad_string==""){
			$student['sixgrad_num1']=0;
			$student['sixgrad_num2']=0;
			$student['sixgrad_num3']=0;
			$student['sixgrad_num4']=0;
		}else{
			$student['sixgrad_num1']=db("app_user_log")->where("userId in($sixgrad_string) and createTime>='$cm' and createTime<'$dm'")->count();
			$student['sixgrad_num2']=db("app_user_log")->where("userId in($sixgrad_string) and createTime>='$cm2' and createTime<'$dm2'")->count();
			$student['sixgrad_num3']=db("app_user_log")->where("userId in($sixgrad_string) and createTime>='$cm3' and createTime<'$dm3'")->count();
			$student['sixgrad_num4']=db("app_user_log")->where("userId in($sixgrad_string) and createTime>='$cm4' and createTime<'$dm4'")->count();
		}
		/*老师*/
		$teacher['week1']=$teacher['totaluse_num'];
		$teacher['week2']=db("app_user_log")->where("userId in($allid) and createTime>='$cm2' and createTime<'$dm2'")->count();
		$teacher['week3']=db("app_user_log")->where("userId in($allid) and createTime>='$cm3' and createTime<'$dm3'")->count();
		$teacher['week4']=db("app_user_log")->where("userId in($allid) and createTime>='$cm4' and createTime<'$dm4'")->count();
		/*课题*/
		$classdata['week1']=db("record_class")->where("t_user_id in($allid) and status in(1,2) and create_time>='$cm' and create_time<'$dm'")->count();
		$classdata['week2']=db("record_class")->where("t_user_id in($allid) and status in(1,2) and create_time>='$cm2' and create_time<'$dm2'")->count();
		$classdata['week3']=db("record_class")->where("t_user_id in($allid) and status in(1,2) and create_time>='$cm3' and create_time<'$dm3'")->count();
		$classdata['week4']=db("record_class")->where("t_user_id in($allid) and status in(1,2) and create_time>='$cm4' and create_time<'$dm4'")->count();		
		/*素材*/
		$material['week1']=$material['weekmaterial'];
		$material['week2']=db("material")->where("userId in($string) and createTime>='$cm2' and createTime<'$dm2'")->count();
		$material['week3']=db("material")->where("userId in($string) and createTime>='$cm3' and createTime<'$dm3'")->count();
		$material['week4']=db("material")->where("userId in($string) and createTime>='$cm4' and createTime<'$dm4'")->count();				
		$material['y_week1']=db("anser")->where("userId in($string) and createTime>='$cm' and createTime<'$dm'")->sum("activation_num");
		$material['y_week2']=db("anser")->where("userId in($string) and createTime>='$cm2' and createTime<'$dm2'")->sum("activation_num");
		$material['y_week3']=db("anser")->where("userId in($string) and createTime>='$cm3' and createTime<'$dm3'")->sum("activation_num");
		$material['y_week4']=db("anser")->where("userId in($string) and createTime>='$cm4' and createTime<'$dm4'")->sum("activation_num");
		// print_r($student);	
		// print_r($teacher);
		// print_r($classdata);
		// print_r($material);	
		$this->assign('student', $student);
		$this->assign('teacher', $teacher);
		$this->assign('classdata', $classdata);
        $this->assign('material', $material);
		return $this->fetch('teacher@count/index');
	}
	/*学生数据*/
    public function student()	
    {	
		$keywords=$this->request->param('keywords');
		$where=[];
		if(!empty($keywords)){			
			$where['realName']=['like','%'.$keywords.'%'];
		}
		$schoolID=db("app_user")->field("schoolID")->where(['id'=>$this->user_id])->find();
		$data=db("app_user")->field("id,realName")->where(['schoolID'=>$schoolID['schoolID'],'role'=>1])->where($where)->order("id desc")->paginate(10);//得到所有学生
		$dataarray=$data->items();
		$map['format'] = array('in','3,4');
		foreach($dataarray as $v=>$val){
			$dataarray[$v]['material_total']=db("material")->where(['userId'=>$val['id']])->count();//素材总数
			$dataarray[$v]['material_txt_total']=db("material")->where(['userId'=>$val['id'],'format'=>0])->count();//文
			$dataarray[$v]['material_pic_total']=db("material")->where(['userId'=>$val['id'],'format'=>1])->count();//图			
			$dataarray[$v]['material_sound_total']=db("material")->where(['userId'=>$val['id']])->where($map)->count();//音
			$dataarray[$v]['material_video_total']=db("material")->where(['userId'=>$val['id'],'format'=>2])->count();//影
			//课题任务数
			$dataarray[$v]['class_task']=db("mytask")->where(['uid'=>$val['id']])->count();
			//参与小组
			$dataarray[$v]['group']=db("relation_student")->where(['user_id'=>$val['id'],'status'=>3])->count();
			//参与活动主题班
			$dataarray[$v]['subject']=db("relation_student")->where(['user_id'=>$val['id'],'status'=>2])->count();
		}
		//print_r($dataarray);
		$this->assign('keywords', $keywords);
		$this->assign('dataarray', $dataarray);
		$this->assign('page', $data->render());
        $this->assign('targets', $this->targets);
        return $this->fetch();
    }
	//学生详情
	public function detail(){
		$id=$this->request->param('id', 0, 'intval');
		$data=db("record")->where(['s_user_id'=>$id,'status'=>2])->order("remind_id desc")->paginate(10);
		$dataarray=$data->items();
		foreach($dataarray as $k=>$v){
			$gdata=db("template_group")->where(['group_id'=>$v['group_id']])->find();
			$tdata=db("template")->field("name")->where(['id'=>$gdata['template_id']])->find();
			$dataarray[$k]['classname']=$tdata['name'];//课题模板
			$tcdata=db("template_category")->field("name")->where(['category_id'=>$gdata['category_id']])->find();
			$dataarray[$k]['subjectname']=$tcdata['name'];//参与主体活动
			$adata=db("app_user")->field("realName")->where(['id'=>$v['t_user_id']])->find();
			$dataarray[$k]['techername']=$adata['realName'];//教师姓名
			// $sc_task=db("mytask")->field("id")->where(['uid'=>$id,'id'=>$v['mytask_id']])->select()->toArray();
			
			// foreach($sc_task as $k=>$m){
				// $mdata[]=$m['id'];
			// }
			// $newmdata=implode(",",$mdata);
			 
			$sucainum=db("anser")->where(['taskid'=>$v['mytask_id']])->sum("activation_num");
			if(empty($sucainum)){
				$dataarray[$k]['material_num']=0;
			}else{
				$dataarray[$k]['material_num']=$sucainum;
			}
			
		}	

		$this->assign('dataarray', $dataarray);
		$this->assign('page', $data->render());
        $this->assign('targets', $this->targets);
		return $this->fetch();	
	}
	//教师数据
	public function teacher()
    {	
		$keywords=$this->request->param('keywords');
		$where=[];
		if(!empty($keywords)){			
			$where['realName']=['like','%'.$keywords.'%'];
		}
		$schoolID=db("app_user")->field("schoolID")->where(['id'=>$this->user_id])->find();
		$data=db("app_user")->field("id,realName")->where(['schoolID'=>$schoolID['schoolID'],'role'=>2])->where($where)->order("id desc")->paginate(10);//得到所有老师
		
		$dataarray=$data->items();
		foreach($dataarray as $v=>$val){
			//课题模板数
			$dataarray[$v]['template_group']=db("template_group")->where(['user_id'=>$val['id']])->count();
			//发放模板次数
			$dataarray[$v]['record']=db("record")->where(['t_user_id'=>$val['id']])->count();
			//指导信息
			//$dataarray[$v]['subject']=db("relation_student")->where(['user_id'=>$val['id']])->count();
		}
		//print_r($dataarray);
		$this->assign('keywords', $keywords);
		$this->assign('dataarray', $dataarray);
		$this->assign('page', $data->render());
        $this->assign('targets', $this->targets);
        return $this->fetch();
    }
	//教师详情
	public function look(){
		$id=$this->request->param('id', 0, 'intval');
		$data=db("record_class")->where(['t_user_id'=>$id,'status'=>2])->order("record_class_id desc")->paginate(10);
		$dataarray=$data->items();
		foreach($dataarray as $k=>$v){
			$gdata=db("template_group")->where(['group_id'=>$v['group_id']])->find();
			$tdata=db("template")->field("name")->where(['id'=>$gdata['template_id']])->find();
			$dataarray[$k]['classname']=$tdata['name'];//课题模板
			$tcdata=db("template_category")->field("name")->where(['category_id'=>$gdata['category_id']])->find();
			$dataarray[$k]['subjectname']=$tcdata['name'];//参与主体活动
			//发放次数
			$dataarray[$k]['recordcount']=db("record")->where(['t_user_id'=>$id,'record_class_id'=>$v['record_class_id']])->count();
		}	
		$this->assign('dataarray', $dataarray);
		$this->assign('page', $data->render());
        $this->assign('targets', $this->targets);
		return $this->fetch();
	}
	//课题数据
	public function classdata()
    {	
		$allid= $this->install();
		$data=db("record_class")->alias('m')->join("relation_class c",'m.class_id=c.class_id','left')->join("app_user a",'m.t_user_id=a.id','left')->field("m.record_class_id,m.class_id,m.create_time,m.group_id,m.t_user_id,a.realName,c.class_name")->where("m.t_user_id in($allid) and m.status=2")->order("m.record_class_id desc")->paginate(10);//得到所有课题
		$dataarray=$data->items();
		foreach($dataarray as $v=>$val){
			//发放学生
			$dataarray[$v]['student_num']=db("record")->where(['record_class_id'=>$val['record_class_id'],'status'=>2])->count();
			//研究小组
			$dataarray[$v]['group_num']=db("relation_class")->where(['p_classid'=>$val['class_id']])->count();
			//素材总数
			//$sc=db("template_group")->field("category_id")->where(['group_id'=>$val['group_id']])->find();
			$sc_task=db("mytask")->field("id")->where(['group_id'=>$val['group_id']])->select()->toArray();
			foreach($sc_task as $k=>$m){
				$mdata[]=$m['id'];
			}
			$newmdata=implode(",",$mdata);
			$dataarray[$v]['material_num']=db("anser")->where("taskid in($newmdata)")->sum("activation_num");
			//课题模板名称
			$aa=db("template_group")->alias('t')->join("template p",'t.template_id=p.id','left')->where(['t.group_id'=>$val['group_id']])->field("p.name")->find();
			$dataarray[$v]['template_name']=$aa['name'];
		}
		//print_r($dataarray);
		$this->assign('dataarray', $dataarray);
		$this->assign('page', $data->render());
        $this->assign('targets', $this->targets);
        return $this->fetch();
    }
 
	
}
